from PIL import Image
import face_recognition

if __name__ == '__main__':
    image = face_recognition.load_image_file("demo.jpg")
    pil_img = Image.fromarray(image)
    pil_img.show()
    face_locations = face_recognition.face_locations(image)

    # 打印：我从图片中找到了 多少 张人脸
    print("I found {} face(s) in this photograph.".format(len(face_locations)))

    # 循环找到的所有人脸
    for face_location in face_locations:
        # 打印每张脸的位置信息
        top, right, bottom, left = face_location
        print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom,
                                                                                                    right))

        # 指定人脸的位置信息，然后显示人脸图片
        face_image = image[top:bottom, left:right]
        pil_image = Image.fromarray(face_image)
        pil_image.show()